﻿using System;

public class Customer
{
    private int id;
    private string name;
    private string phone;
    private string email;
    private string address;

    public Customer(int id, string name, string phone, string email, string address)
    {
        // 验证输入参数
        if (id <= 0)
            throw new ArgumentException("ID必须大于0");

        if (string.IsNullOrWhiteSpace(name))
            throw new ArgumentException("姓名不能为空");

        this.id = id;
        this.name = name ?? ""; // 如果为null则设为空字符串
        this.phone = phone ?? "";
        this.email = email ?? "";
        this.address = address ?? "";

        Console.WriteLine($"客户对象创建成功: ID={id}, Name={name}");
    }

    // 属性保持不变...
    public int Id { get => id; set => id = value; }
    public string Name { get => name; set => name = value; }
    public string Phone { get => phone; set => phone = value; }
    public string Email { get => email; set => email = value; }
    public string Address { get => address; set => address = value; }

    public void DisplayInfo()
    {
        Console.WriteLine($"ID: {id}, 姓名: {name}, 电话: {phone}, 邮箱: {email}, 地址: {address}");
    }
}

public class CustomerManager
{
    private Customer[] customers;
    private static int customerCount = 0;
    private int maxCapacity;

    public CustomerManager(int capacity)
    {
        Console.WriteLine($"正在初始化CustomerManager，容量: {capacity}");

        if (capacity <= 0)
        {
            capacity = 10;
            Console.WriteLine("容量已调整为默认值10");
        }

        maxCapacity = capacity;
        customers = new Customer[capacity];

        // 显式初始化数组
        for (int i = 0; i < capacity; i++)
        {
            customers[i] = null;
        }

        Console.WriteLine($"CustomerManager初始化完成，实际容量: {customers.Length}");
    }

    public bool AddCustomer(Customer customer)
    {
        Console.WriteLine($"尝试添加客户，当前客户数: {customerCount}, 最大容量: {maxCapacity}");

        if (customer == null)
        {
            Console.WriteLine("错误：尝试添加空客户对象");
            return false;
        }

        if (customerCount >= maxCapacity)
        {
            Console.WriteLine($"错误：客户数量已达上限 {maxCapacity}");
            return false;
        }

        if (customerCount >= customers.Length)
        {
            Console.WriteLine($"错误：数组索引越界，customerCount={customerCount}, 数组长度={customers.Length}");
            return false;
        }

        try
        {
            customers[customerCount] = customer;
            customerCount++;
            Console.WriteLine($"客户添加成功，新客户数: {customerCount}");
            return true;
        }
        catch (Exception ex)
        {
            Console.WriteLine($"添加客户时发生异常: {ex.Message}");
            Console.WriteLine($"堆栈跟踪: {ex.StackTrace}");
            return false;
        }
    }
    public bool DeleteCustomer(int id)
    {
        for (int i = 0; i < customerCount; i++)
        {
            if (customers[i].Id == id)
            {
                // 将后面的元素前移
                for (int j = i; j < customerCount - 1; j++)
                {
                    customers[j] = customers[j + 1];
                }
                customerCount--;
                customers[customerCount] = null; // 清空最后一个元素
                return true;
            }
        }
        return false;
    }

    // 修改客户信息
    public bool UpdateCustomer(int id, string name, string phone, string email, string address)
    {
        for (int i = 0; i < customerCount; i++)
        {
            if (customers[i].Id == id)
            {
                customers[i].Name = name;
                customers[i].Phone = phone;
                customers[i].Email = email;
                customers[i].Address = address;
                return true;
            }
        }
        return false;
    }

    // 查询客户
    public Customer FindCustomer(int id)
    {
        foreach (Customer customer in customers)
        {
            if (customer != null && customer.Id == id)
                return customer;
        }
        return null;
    }

    // 查询所有客户
    public void DisplayAllCustomers()
    {
        Console.WriteLine("\n=== 所有客户信息 ===");
        for (int i = 0; i < customerCount; i++)
        {
            customers[i].DisplayInfo();
        }
        Console.WriteLine($"总计: {customerCount} 个客户");
    }

    // 获取客户数量
    public static int GetCustomerCount()
    {
        return customerCount;
    }
}
 

    // 其他方法保持不变...
   
public class Program
{
    public static void Main(string[] args)
    {
        try
        {
            Console.WriteLine("程序启动...");
            CustomerManager manager = new CustomerManager(10);

            // 添加初始测试数据
            Console.WriteLine("添加初始测试数据...");
            manager.AddCustomer(new Customer(1, "藏头诗", "13800138000", "zhangsan@email.com", "北京市朝阳区"));
            manager.AddCustomer(new Customer(2, "柴鸡蛋", "13900139000", "lisi@email.com", "上海市浦东新区"));
            manager.AddCustomer(new Customer(3, "顺导", "13700137000", "wangwu@email.com", "广州市天河区"));

            bool running = true;
            while (running)
            {
                ShowMenu();
                string choice = Console.ReadLine();

                switch (choice)
                {
                    case "1":
                        manager.DisplayAllCustomers();
                        break;
                    case "2":
                        AddCustomer(manager);
                        break;
                    case "3":
                        DeleteCustomer(manager);
                        break;
                    case "4":
                        UpdateCustomer(manager);
                        break;
                    case "5":
                        FindCustomer(manager);
                        break;
                    case "6":
                        running = false;
                        Console.WriteLine("谢谢使用！");
                        break;
                    default:
                        Console.WriteLine("无效选择，请重新输入！");
                        break;
                }
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine($"程序发生未处理异常: {ex.Message}");
            Console.WriteLine($"异常类型: {ex.GetType()}");
            Console.WriteLine($"堆栈跟踪: {ex.StackTrace}");
            Console.WriteLine("按任意键退出...");
            Console.ReadKey();
        }
    }

    private static void ShowMenu()
    {
        Console.WriteLine("\n=== 客户信息管理系统 ===");
        Console.WriteLine("1. 显示所有客户");
        Console.WriteLine("2. 添加客户");
        Console.WriteLine("3. 删除客户");
        Console.WriteLine("4. 修改客户");
        Console.WriteLine("5. 查询客户");
        Console.WriteLine("6. 退出系统");
        Console.Write("请选择操作: ");
    }

    private static void AddCustomer(CustomerManager manager)
    {
        Console.WriteLine("\n--- 添加客户 ---");
        try
        {
            Console.Write("输入客户ID: ");
            string idInput = Console.ReadLine();

            if (!int.TryParse(idInput, out int id) || id <= 0)
            {
                Console.WriteLine("错误：ID必须是大于0的数字！");
                return;
            }

            // 检查ID是否重复
            // 注意：这里需要先实现FindCustomer方法
            /*
            if (manager.FindCustomer(id) != null)
            {
                Console.WriteLine("错误：该ID已存在！");
                return;
            }
            */

            Console.Write("输入姓名: ");
            string name = Console.ReadLine();
            if (string.IsNullOrWhiteSpace(name))
            {
                Console.WriteLine("错误：姓名不能为空！");
                return;
            }

            Console.Write("输入电话: ");
            string phone = Console.ReadLine();

            Console.Write("输入邮箱: ");
            string email = Console.ReadLine();

            Console.Write("输入地址: ");
            string address = Console.ReadLine();

            Console.WriteLine("正在创建客户对象...");
            Customer customer = new Customer(id, name, phone, email, address);

            Console.WriteLine("正在添加客户到管理器...");
            if (manager.AddCustomer(customer))
            {
                Console.WriteLine("客户添加成功！");
            }
            else
            {
                Console.WriteLine("客户添加失败！");
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine($"添加客户时发生错误: {ex.Message}");
            Console.WriteLine($"错误详情: {ex.StackTrace}");
        }
    }

    // 其他方法保持不变...
    private static void DeleteCustomer(CustomerManager manager)
    {
        Console.WriteLine("\n--- 删除客户 ---");
        try
        {
            // 先显示所有客户，方便用户选择
            manager.DisplayAllCustomers();

            Console.Write("\n输入要删除的客户ID: ");
            string input = Console.ReadLine();

            if (!int.TryParse(input, out int id))
            {
                Console.WriteLine("错误：ID必须是数字！");
                return;
            }

            // 先查找客户，确认存在
            Customer customerToDelete = manager.FindCustomer(id);
            if (customerToDelete != null)
            {
                Console.WriteLine($"找到客户: ID={customerToDelete.Id}, 姓名={customerToDelete.Name}");
                Console.Write("确认删除吗？(y/n): ");
                string confirm = Console.ReadLine();

                if (confirm?.ToLower() == "y")
                {
                    if (manager.DeleteCustomer(id))
                    {
                        Console.WriteLine("客户删除成功！");
                    }
                    else
                    {
                        Console.WriteLine("客户删除失败！");
                    }
                }
                else
                {
                    Console.WriteLine("取消删除操作");
                }
            }
            else
            {
                Console.WriteLine($"未找到ID为 {id} 的客户！");
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine($"删除客户时发生错误: {ex.Message}");
            Console.WriteLine($"错误详情: {ex.StackTrace}");
        }
    }

    private static void UpdateCustomer(CustomerManager manager)
    {
        Console.WriteLine("\n--- 修改客户信息 ---");
        try
        {
            // 先显示所有客户
            manager.DisplayAllCustomers();

            Console.Write("\n输入要修改的客户ID: ");
            string idInput = Console.ReadLine();

            if (!int.TryParse(idInput, out int id))
            {
                Console.WriteLine("错误：ID必须是数字！");
                return;
            }

            // 先查找客户，确认存在
            Customer customerToUpdate = manager.FindCustomer(id);
            if (customerToUpdate != null)
            {
                Console.WriteLine($"当前客户信息:");
                customerToUpdate.DisplayInfo();

                Console.WriteLine("\n请输入新的信息（直接回车保持原值）:");

                Console.Write($"新姓名 ({customerToUpdate.Name}): ");
                string name = Console.ReadLine();
                if (string.IsNullOrWhiteSpace(name))
                {
                    name = customerToUpdate.Name; // 保持原值
                }

                Console.Write($"新电话 ({customerToUpdate.Phone}): ");
                string phone = Console.ReadLine();
                if (string.IsNullOrWhiteSpace(phone))
                {
                    phone = customerToUpdate.Phone;
                }

                Console.Write($"新邮箱 ({customerToUpdate.Email}): ");
                string email = Console.ReadLine();
                if (string.IsNullOrWhiteSpace(email))
                {
                    email = customerToUpdate.Email;
                }

                Console.Write($"新地址 ({customerToUpdate.Address}): ");
                string address = Console.ReadLine();
                if (string.IsNullOrWhiteSpace(address))
                {
                    address = customerToUpdate.Address;
                }

                if (manager.UpdateCustomer(id, name, phone, email, address))
                {
                    Console.WriteLine("客户信息修改成功！");
                    Console.WriteLine("修改后的信息:");
                    manager.FindCustomer(id)?.DisplayInfo();
                }
                else
                {
                    Console.WriteLine("客户信息修改失败！");
                }
            }
            else
            {
                Console.WriteLine($"未找到ID为 {id} 的客户！");
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine($"修改客户时发生错误: {ex.Message}");
            Console.WriteLine($"错误详情: {ex.StackTrace}");
        }
    }

    // 修复：添加完整

    iprivate static void FindCustomer(CustomerManager manager)
    {
        Console.WriteLine("\n--- 查询客户 ---");
        try
        {
            Console.Write("输入要查询的客户ID: ");
            string input = Console.ReadLine();

            if (!int.TryParse(input, out int id))
            {
                Console.WriteLine("错误：ID必须是数字！");
                return;
            }

            Customer customer = manager.FindCustomer(id);
            if (customer != null)
            {
                Console.WriteLine("\n查询结果：");
                customer.DisplayInfo();
            }
            else
            {
                Console.WriteLine($"未找到ID为 {id} 的客户！");
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine($"查询客户时发生错误: {ex.Message}");
            Console.WriteLine($"错误详情: {ex.StackTrace}");
        }
    }
}

